<?php

class Authority {

    private $db;

    public function __construct($db) {
        if (is_object($db)) {
            $this->db = $db;
        } else {
            die('Error! Authority construct need D3sql Object! Demo:  new Authority(- Add D3Sql() Object -)');
        }
    }

    /**
     * Create New Group And Adding Authority
     * 
     * @param mixed $groupName
     * @param mixed $groupAuths
     */
    public function createGroup($groupName, $groupAuths) {
        $checkGroupNameSQLText = " SELECT count(*) as toplam FROM e_authority_group WHERE group_name = '" . $this->db->escape($groupName) . "' ";
        $checkGroupNameSQL = $this->db->sql($checkGroupNameSQLText);
        $checkGroupName = $this->db->getir($checkGroupNameSQL);

        if ($checkGroupName['toplam'] == 0) {
            $createReturn = $this->db->sql("
												 INSERT INTO e_authority_group 
												 (
													group_name
												 )
												 VALUES
												 (
													'" . $this->db->escape($groupName) . "'
												 )
											  ");

            if ($createReturn) {
                $return['sonuc'] = true;
                $return['sonuc'] = $groupName . ' isimli grup oluşturuldu.';

                if (is_array($groupAuths)) {
                    $lastID = $this->db->sonid();
                    foreach ($groupAuths as $auth) {
                        $this->db->sql(" 
											INSERT INTO e_authority 
											(
												method,
												group_id
											) 
											VALUES 
											(
												'" . $auth . "',
												" . $lastID . "
											) 
										  ");
                    }
                }
            } else {
                $return['sonuc'] = false;
                $return['aciklama'] = 'Sorgulama hatası oluştu. Grup Kurulamadı!. Lütfen teknik destek ekibimiz ile ileitşime geçiniz.';
            }
        } else {
            $return['sonuc'] = false;
            $return['aciklama'] = 'Aynı isimde bir grup mevcut! Lütfen farklı bir isim tanımlayarak yeniden deneyiniz.';
        }

        return $return;
    }

    /**
     * Check User Authority
     * 
     * @param mixed $authority
     * @param mixed $userLoginName
     */
    public function checkAuthority($authority, $userLoginName) {
        $checkAuthoritySQLText = " 	SELECT count(*) as toplam FROM e_shop_admin u 
                                        LEFT JOIN e_authority_group ag ON u.authority_group_id = ag.id 
                                        LEFT JOIN e_authority a ON ag.id = a.group_id 
                                        WHERE u.login = '" . $userLoginName . "' 
                                        AND a.method = '" . $authority . "'  ";
        $checkAuthoritySQL = $this->db->sql($checkAuthoritySQLText);
        $checkAuthorit = $this->db->getir($checkAuthoritySQL);
        if ($checkAuthorit['toplam'] > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * get Group List
     * 
     * @param mixed $groupID
     */
    public function getGroups($groupID = false) {
        $groupSQLText = " SELECT g.*, (SELECT count(*) FROM e_shop_admin u WHERE u.authority_group_id = g.id ) as  toplam FROM e_authority_group g  ";
        if ($groupID) {
            $groupSQLText .= " WHERE g.id =" . $groupID;
        }
        $groupSQLText .= " ORDER BY g.group_name ASC ";


        $groupSQL = $this->db->sql($groupSQLText);
        $groupArray = array();
        while ($group = $this->db->getir($groupSQL)) {
            $groupArray[] = $group;
        }

        return $groupArray;
    }

    /**
     * get Group Authority List
     * 
     * @param mixed $groupID
     */
    public function getGroupAuthority($groupID) {
        $groupAuthoritySQLText = "
SELECT
 a.method,
 al.note  
FROM e_authority a LEFT JOIN e_authority_list al ON a.method = al.method 
WHERE a.group_id =" . $groupID;

        $groupAuthoritySQL = $this->db->sql($groupAuthoritySQLText);
        $groupAuthorityArray = array();
        while ($groupAuthority = $this->db->getir($groupAuthoritySQL)) {
            $groupAuthorityArray[] = $groupAuthority;
        }

        return $groupAuthorityArray;
    }

    /**
     * get All Authortiy List and Note
     * 
     */
    public function getAuthorityList() {
        $authorityListSQLText = "
									  SELECT
									   * 
									  FROM e_authority_list 
									";
        $authorityListSQL = $this->db->sql($authorityListSQLText);
        $authorityListArray = array();
        while ($authorityList = $this->db->getir($authorityListSQL)) {
            $authorityListArray[] = $authorityList;
        }

        return $authorityListArray;
    }

}

?>
